package com.rover.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grid.DataGrid;
import com.grid.Page;
import com.rover.system.entity.Role;
import com.rover.system.service.RoleService;
import com.utils.HqlGenerateUtil;
import com.utils.JsonUtil;

/**
 * 角色
 */
@Controller
@RequestMapping("/roleController")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/query")
    public @ResponseBody
    Map<String, Object> query(HttpServletRequest request, Page page,
                              HttpServletResponse response) throws Exception {
        HqlGenerateUtil.installHql(request, Role.class, page);
        Map<String, Object> map = new HashMap<String, Object>();
        DataGrid dataGrid =  roleService.query(page);
        map = JsonUtil.JSONString(dataGrid.getResult(),
                dataGrid.getTotalCount());

        return map;
    }

    @RequestMapping(value = "/getList")
    public @ResponseBody
    Map<String, Object> getList(HttpServletRequest request,
                              HttpServletResponse response) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();
        map = JsonUtil.JSONString(roleService.geList(),    0l);

        return map;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> save(Role role, HttpServletRequest request)
            throws Exception {
        // UserSession u = (UserSession)
        // request.getSession().getAttribute("userSession");
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean success = true;
        String message = "";
        try {
            if (role.getId() == null) {
                if (roleService.isAddNameRepeat(role.getName())) {
                    success = false;
                    message = "此角色名称已存在，请重新输入！";
                }
            } else if (roleService.isEditNameRepeat(role.getName(),
                    role.getId())) {
                success = false;
                message = "此角色名称已存在，请重新输入！";
            }
            // 判断是否验证成功
            if (success) {
                this.roleService.save(role);

            }
            map.put("success", success);
            map.put("message", message);
        } catch (Exception e) {
            // logger.error(e.getMessage(), e);
            e.printStackTrace();
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> update(Role role, HttpServletRequest request)
            {
        // UserSession u = (UserSession)
        // request.getSession().getAttribute("userSession");
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean success = true;
        String message = "";
        try {
           if (roleService.isEditNameRepeat(role.getName(),
                    role.getId())) {
                success = false;
                message = "此角色名称已存在，请重新输入！";
            }
            // 判断是否验证成功
           if (success) {
                this.roleService.update(role);

           }
            map.put("success", success);
            map.put("message", message);
        } catch (Exception e) {
            // logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> delete(@RequestParam("id") List<Integer> id) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map.put("success", true);
            map.put("message", "");
            for(Integer roleId : id){
                roleService.delete(roleId);
            }
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", "正在使用中的角色不能删除！");
        }
        return map;
    }

    @RequestMapping(value = "/getRoleResource", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> getRoleResource(@RequestParam("id") String id) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            map.put("success", true);
            map.put("data", roleService.getRoleResourceName(Integer.valueOf(id)));

        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/getRoleTree", method = RequestMethod.POST)
    @ResponseBody
    public   Map<String, Object> getRoleTree(@RequestParam("id") String id) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            map.put("success", true);
            map.put("data", roleService.getRoleResourceName(Integer.valueOf(id)));

        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

}
